package com.phison.fenestrapro.dashboard.util;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.LinkedHashSet;

import com.invient.vaadin.charts.InvientCharts.DecimalPoint;
import com.invient.vaadin.charts.InvientCharts.Series;

/**
 *
 * 
 * @author Niall O'Hara
 *
 */
public class Functions {
	/**
	 * @param d
	 *            double to be rounded
	 * @param n
	 *            number of decimal places
	 * @return 
	 * @return rounded double n decimal places
	 */
	public double roundDecimal(double d, int n) {
		int multiplier = (int) Math.pow(10, n);
		double newNum = Math.floor(d * multiplier + 0.5) / multiplier;
		return newNum;
	}
	
	public final Class<?>[] ARRAY_PRIMITIVE_TYPES = { 
	        int[].class, float[].class, double[].class, boolean[].class, 
	        byte[].class, short[].class, long[].class, char[].class };

	public Object[] getArray(Object val){
	    Class<?> valKlass = val.getClass();
	    Object[] outputArray = null;

	    for(Class<?> arrKlass : ARRAY_PRIMITIVE_TYPES){
	        if(valKlass.isAssignableFrom(arrKlass)){
	            int arrlength = Array.getLength(val);
	            outputArray = new Object[arrlength];
	            for(int i = 0; i < arrlength; ++i){
	                outputArray[i] = Array.get(val, i);
	                            }
	            break;
	        }
	    }
	    if(outputArray == null) // not primitive type array
	        outputArray = (Object[])val;

	    return outputArray;
	}
	
    @SuppressWarnings("unused")
	private static LinkedHashSet<DecimalPoint> getPoints(Series series, double... values) {
        LinkedHashSet<DecimalPoint> points = new LinkedHashSet<DecimalPoint>();
        for (double value : values) {
            points.add(new DecimalPoint(series, value));
        }
        return points;
    }

    @SuppressWarnings("unused")
	private static LinkedHashSet<DecimalPoint> getPoints(Series series, double[]... values) {
        LinkedHashSet<DecimalPoint> points = new LinkedHashSet<DecimalPoint>();
        for (double[] value : values) {
            Double x, y = null;
            if (value.length == 0)
                continue;
            if (value.length == 2) {
                x = value[0];
                y = value[1];
            } else {
                x = value[0];
            }
            points.add(new DecimalPoint(series, x, y));
        }
        return points;
    }
    
    public static Object[] genWindowDimenstionsObjectArray(String facade, int floors) {
    	ArrayList<String> a = new ArrayList<String>();
		a.add("floor");
		a.add("windowHH");
		a.add("windowCH");
		a.add("windowRV");
		
		String leadingZero = null;
		
		for (int i = 0; i < floors; i++) {
			a.add("floor" + i);
			if (i < 10) {
				leadingZero = "0";
			} else {
				leadingZero = "";
			}
			a.add("facade" + facade + "WindowHH" + leadingZero + i);
			a.add("facade" + facade + "WindowCH" + leadingZero + i);
			a.add("facade" + facade + "WindowRV" + leadingZero + i);
		}
		Object[] o = a.toArray();
		return o;
    }
    	
}
